package com.bignerdranch.android.myfresco;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

public class MainActivity extends AppCompatActivity {

    private SimpleDraweeView image_view;
    Uri uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Fresco.initialize(this);
        setContentView(R.layout.activity_main);
        initView();
//   动态图片     uri = Uri.parse("http://img5.duitang.com/uploads/item/201207/14/20120714201359_E4VdT.thumb.700_0.gif");
//        uri = Uri.parse("https://support.files.wordpress.com/2009/07/pigeony.jpg?w=688");
//        uri = Uri.parse("http://img1.3lian.com/2015/w7/66/d/2.jpg");
//        uri = Uri.parse("http://img3.3lian.com/2014/c1/29/d/26.jpg");
//        uri = Uri.parse("http://img2.imgtn.bdimg.com/it/u=1682155141,2896228739&fm=21&gp=0.jpg");
//        uri = Uri.parse("http://img0.imgtn.bdimg.com/it/u=978721559,3137539165&fm=21&gp=0.jpg");
//        uri = Uri.parse("http://img.newyx.net/newspic/image/201502/03/9a12fd460e.jpg");
        uri = Uri.parse("http://image55.360doc.com/DownloadImg/2012/09/1618/26871610_1.jpg");
//        image_view.setImageURI(uri);
//   显示动图     initGif();
        requestImage();
        image_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"cool",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView() {
        image_view = (SimpleDraweeView) findViewById(R.id.image_view);
    }
    private void initGif(){
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(uri)
                .setAutoPlayAnimations(true)
                .build();
        image_view.setController(controller);

    }


    private void requestImage(){
        ImageRequest request = ImageRequestBuilder
                .newBuilderWithSource(uri)
                .setProgressiveRenderingEnabled(true)
                .build();
        PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(image_view.getController())
                .build();


        image_view.setController(controller);
    }
}
